﻿using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Data.Entity.Infrastructure;
using System.Data.Entity.ModelConfiguration.Conventions;
using ESoft.Core.DAO;
using ESoft.Core.Initialize;
using ESoft.Core.Logging;

namespace ESoft.Core.Model
{
    public class ESoftDbContext : DbContext
    {
        private readonly Logging.ILogger logger = LogManager.GetLogger(typeof(ESoftDbContext));
        public ESoftDbContext(String connect)
            : base(connect)
        {
            //启动不再效验__MigrationHistory表
            Database.SetInitializer<ESoftDbContext>(null);
           // this.Database.Initialize(false);
            base.Database.Log = log=> {
                logger.Info("SQL日志："+log);
            };
        }

        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {   
            //移除一对多的级联删除
            modelBuilder.Conventions.Remove<OneToManyCascadeDeleteConvention>();

            //注册实体配置信息
            ICollection<IEntityMapper> entityMappers = DatabaseInitializer.EntityMappers;
            foreach (IEntityMapper mapper in entityMappers)
            {
                mapper.RegistTo(modelBuilder.Configurations);
            }
        }
    }
}
